﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using WinnerCMS.Common;
using System.ComponentModel;

#endregion

namespace WinnerCMS.BLL
{
    [Serializable, DataContract]
    public partial class CMSConfig
    {
        private CMSConfig()
        {
            FileTypes = new[] {".zip", ".rar", ".dwg", ".doc", ".chm", ".pdf", ".gif", ".jpg", ".png", ".swf"};
            ThreadSleepTime = 200;
            ThreadNum = 2;

            UpLoadFolder = @"~\UpLoadFolder\";

            Soft = new SoftConfig();
            Member = new MemberConfig();
            Scheduler = new SchedulerConfig();
            Html = new HtmlConfig();
            GuestBook = new GuestBookConfig();
            Friend = new FriendLinkConfig();
            EMail = new EMailConfig();
            DrawingWater = new DrawingWaterConfig();
            DefendPROC = new DefendPROCConfig();
            Article = new ArticleConfig();
            Counter = new CounterConfig();
            EnableLucene = true;

            FullTextIndexPath = @"E:\FullTextIndex";
        }

        /// <summary>
        /// 文章模型
        /// </summary>
        [DisplayName("文章模型"), Category("Tab"), DataMember]
        public ArticleConfig Article { get; set; }

        /// <summary>
        /// 访问统计
        /// </summary>
        [DisplayName("访问统计"), Category("Tab"), DataMember]
        public CounterConfig Counter { get; set; }

        /// <summary>
        /// 防垃圾策略 
        /// </summary>
        [DisplayName("防垃圾策略"), Category("Tab"), DataMember]
        public DefendPROCConfig DefendPROC { get; set; }

        [DisplayName("图片水印设置"), Category("Tab"), DataMember]
        public DrawingWaterConfig DrawingWater { get; set; }

        /// <summary>
        /// 邮件设置
        /// </summary>
        [DisplayName("邮件设置"), Category("Tab"), DataMember]
        public EMailConfig EMail { get; set; }

        [DisplayName("友情链接"), Category("Tab"), DataMember]
        public FriendLinkConfig Friend { get; set; }

        [DisplayName("留言簿"), Category("Tab"), DataMember]
        public GuestBookConfig GuestBook { get; set; }

        /// <summary>
        /// Html生成
        /// </summary>
        [DisplayName("Html生成"), Category("Tab"), DataMember]
        public HtmlConfig Html { get; set; }

        [DisplayName("会员"), Category("Tab"), DataMember]
        public MemberConfig Member { get; set; }

        [DisplayName("任务调度"), Category("Tab"), DataMember]
        public SchedulerConfig Scheduler { get; set; }

        [DisplayName("软件模型"), Category("Tab"), DataMember]
        public SoftConfig Soft { get; set; }

        [DisplayName("文件监控"), Category("Tab"), DataMember]
        public FileWatcherConfig FileWatcher { get; set; }

        /// <summary>
        /// 后台登录是否使用验证码
        /// </summary>
        [DisplayName("后台登录是否使用验证码"), DataMember]
        public bool PassCode { get; set; }

        /// <summary>
        /// 上传文件类型
        /// </summary>
        [DisplayName("上传文件类型"), DataMember]
        public string[] FileTypes { get; set; }

        /// <summary>
        /// 上传路径
        /// </summary>
        [DisplayName("上传路径"), DataMember]
        public string UpLoadFolder { get; set; }

        /// <summary>
        /// 线程数量
        /// </summary>
        [DisplayName("线程数量"), DataMember]
        public int ThreadNum { get; set; }

        /// <summary>
        /// 线程间歇
        /// </summary>
        [DisplayName("线程间歇"), DataMember]
        public int ThreadSleepTime { get; set; }

        /// <summary>
        /// 是否已经安装就绪
        /// </summary>
        [DisplayName("是否已经安装就绪"), DataMember]
        public bool Already { get; set; }

        /// <summary>
        /// 是否启用 Lucene.net 搜索界面
        /// </summary>
        [DisplayName("是否启用Lucene.net"), DataMember]
        public bool EnableLucene { get; set; }


        /// <summary>
        /// 全文索引路径
        /// </summary>
        [DisplayName("全文索引路径"), DataMember]
        public string FullTextIndexPath { get; set; }

        #region 单例配置

        private static readonly object SyncRoot = new Object(); //为了解决多线程，而添加的一个附加变量，以避免死锁

        private static CMSConfig _instance;

        /// <summary>
        /// 实例化配置
        /// </summary>
        /// <returns></returns>
        public static CMSConfig Instance()
        {
            if (_instance == null)
            {
                lock (SyncRoot)
                {
                    if (_instance == null)
                    {
                        _instance =
                            SerializableHelper.DeSerialize<CMSConfig>(
                                new FileInfo(HttpServer.MapPath("~\\Config\\WinnerCMS.config")));

                        if (_instance == null)
                        {
                            throw new FormatException("反序列化出错。");
                        }
                    }
                }
            }

            return _instance;
        }

        #endregion

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="config"></param>
        public void Save(CMSConfig config)
        {
            lock (this)
            {
                SerializableHelper.Serialize(config, HttpServer.MapPath("~/Config/WinnerCMS.config"));
            }
        }
    }
}